import loadJson from "../../utils/loadJson";
import ImagePakData from "./ImagePakData";
import DEMPakData from "./DEMPakData";
import ImageHPYData from "./ImageHPYData";
import DEM_HPYData from "./DEM_HPYData";
import GeoJsonData from "./geojson_data";
import LabelData from "./LabelData";
import BillboardData from "./BillboardData";
import VectorData from "./VectorData";
import KML_data from "./kml_data";
import Tiles_data from "./tiles_data";
import Model_data from "./model_data";
import string from "shapefile/dbf/string";
import axios from "axios"
function LayerManager(viewer) {
    this._viewer = viewer;
    this._jsonData = [];
    this.pathObject = null;
}

LayerManager.__ginstance = null;
LayerManager.BASEIMAGE="BASEIMAGE";
LayerManager.BASEDEM="BASEDEM";
LayerManager.IMAGE_HPY="IMAGE_HPY";
LayerManager.DEM_HPY="DEM_HPY";
LayerManager.LABEL = "LABEL";
LayerManager.BILLBOARD = "BILLBOARD";
LayerManager.VECTOR = "VECTOR";
LayerManager.GEOJSON = "GEOJSON";
LayerManager.KML = "KML";
LayerManager.GLTF = "GLTF";
LayerManager.B3DM = "B3DM";
LayerManager.IMGPAK = "IMGPAK";
LayerManager.GEOJSON_POLYGON = "MultiPolygon";
LayerManager.GEOJSON_POLYLINE = "LineString";
LayerManager.GEOJSON_POINT = "Point";
LayerManager.RootPath = "/storage/emulated/0/";
LayerManager.imgPrefix = "http://mev.com/";
LayerManager.demPrefix = "http://mev.com/";
//新建文件模板
LayerManager.LayerTemplate = [
    {
        name: "基础数据",
        children: []
    },
    {
        name: "地名",
        children: []
    },
    {
        name: "目标",
        children: []
    },
    {
        name: "矢量数据",
        children: []
    }
];

LayerManager.getInstance = function() {
    if (LayerManager.__ginstance == null) {
        LayerManager.__ginstance = new LayerManager(window.cesiumViewer);
    }
    return LayerManager.__ginstance;
};

/*读取图层文件并转换成json对象给每条数据赋加相应类型的数据对象*/
LayerManager.prototype.init = function(path, sdcard, callback) {
    let that = this;
    axios.get(path + "?datatype=json").then(function(data){
        let basePath = path.slice(0, path.lastIndexOf("/")) + "/";
        data = data.data;
        let uid = 0;
        data.forEach(function(parentItem) {
            let index = 0;
            parentItem.id = uid;
            ++uid;
            parentItem.children.forEach(function(item) {
                item.basePath = basePath;
                item.index = index;
                index += 1;
                item.cesiumTreeData = that.getCesiumTreeData(item);
                item.id = uid;
                ++uid;
                if(item.visible === "true")
                {
                    item.visible = true;
                }
                else if(item.visible === "false")
                {
                    item.visible = false;
                }
                else if(item.visible === undefined)
                {
                    item.visible = false;
                }
            });
        });
        that._jsonData = data;
        callback(data);
    });
};
//给每条数据根据各自类型生成相应的数据对象
LayerManager.prototype.getCesiumTreeData = function(item) {
    switch (item.class.toUpperCase()) {
        case LayerManager.BASEIMAGE:
            return new ImagePakData(item, this._viewer);
        case LayerManager.IMAGE_HPY:
            return new ImageHPYData(item, this._viewer);
        case LayerManager.DEM_HPY:
            return new DEM_HPYData(item, this._viewer);
        case LayerManager.BASEDEM:
            return new DEMPakData(item, this._viewer);
        case LayerManager.LABEL:
            return new LabelData(item, this._viewer);
        case LayerManager.BILLBOARD:
            return new BillboardData(item, this._viewer);
        case LayerManager.VECTOR:
            return new VectorData(item, this._viewer);
        case LayerManager.GEOJSON:
            return new GeoJsonData(item, this._viewer);
        case LayerManager.B3DM:
            return new Tiles_data(item, this._viewer);
        case LayerManager.GLTF:
            return new Model_data(item, this._viewer);
        case LayerManager.KML:
            return new KML_data(item, this._viewer);
    }
};
LayerManager.prototype.updateInternalIndex = function(){
    let internalIndex = {
        primitiveIndex: 0,
        imageryIndex: 1
    };
    let d = this._jsonData;
    d.forEach(function(g) {
        g.children.forEach(function(item) {
            item.cesiumTreeData.updateInternalIndex(internalIndex);
        });
    });
}
/*//设置视角
LayerManager.prototype.setView = function(obj) {
    /!*this._viewer.zoomTo(obj);*!/
    this._viewer.flyTo(obj);
};*/
//将对象中.visible为true都进行展示
LayerManager.prototype.loadData = function() {
    let layerData = this.getLayerData();
    layerData.forEach(function(parentItem) {
        parentItem.children.forEach(function(item) {
            if (item.visible === true) {
                if (item.cesiumTreeData) {
                    item.cesiumTreeData.show();
                }
            }
        });
    });
};
LayerManager.prototype.setVisible = function(item, visible) {
    if (item.cesiumTreeData) {
        if(item.visible === visible)
        {
            return;
        }
        item.visible = visible;
        this.updateInternalIndex();
        if(item.visible)
        {
            item.cesiumTreeData.show();
        }
        else {
            item.cesiumTreeData.close();
        }
    }
};
//将所有数据全部关闭展示
LayerManager.prototype.closeLoadData = function() {
    let d = this._jsonData;
    let that = this;
    d.forEach(function(g) {
        g.children.forEach(function(item) {
            item.cesiumTreeData.setVisible(false, that);
        });
    });
};
//获得图层对象
LayerManager.prototype.getLayerData = function() {
    return this._jsonData;
};
//将对象属性进行设置
LayerManager.prototype.setProperties = function(item, props) {
    item.cesiumTreeData.setProperties(props);
};
//保存图层对象保存到文件里
LayerManager.prototype.saveFile = function(that, layerData) {
    that.$message({
        showClose: true,
        message: "已保存至" + this.pathObject,
        type: "warning"
    });
    this._jsonData = layerData;

    let resultLayer = [];
    layerData.forEach(function(g) {
        let a = {};
        a.name = g.name;
        a.children = [];
        g.children.forEach(function(item) {
            let obj = {};
            obj.datatype = item.datatype;
            obj.id = item.id;
            obj.name = item.name;

            //使用SdcardNumber代替字符
            if (string(obj.datatype) === "IMGPAK" || string(obj.datatype) === "DEM数据") {
                const str = item.path.split("/");
                const index = str[5];
                str[5] = "SdcardNumber";
                item.path = str[0] + "/" + str[1] + "/" + str[2] + "/" + str[3] + "/" + str[4] + "/" + str[5] + "/" + str[6] + "/" + str[7] + "/" + str[8] + "/" + str[9];
            } else {
                const str1 = item.path.split("/");
                const index1 = str1[2];
                str1[2] = "SdcardNumber";
                item.path = str1[0] + "/" + str1[1] + "/" + str1[2] + "/" + str1[3] + "/" + str1[4] + "/" + str1[5] + "/" + str1[6];
            }

            obj.path = item.path;
            obj.visible = item.cesiumTreeData._data.visible;
            if (item.cesiumTreeData.properties) {
                obj.properties = item.cesiumTreeData.properties;
            }
            a.children.push(obj);
        });
        resultLayer.push(a);
    });
    /*resultLayer.forEach(function(g) {
    g.children.forEach(function(item) {
        if (item.datatype === LayerManager.GEOJSON) {
            if (item.properties.type) {
                delete item.properties.type;
            }
        }
    });
    });*/
    JavaContext.saveFile(this.pathObject, JSON.stringify(resultLayer));
};

//替換sd号码
LayerManager.prototype.saveSdcard = function(that, layerData, sdcard) {
    this._jsonData = layerData;
    let resultLayer = [];
    layerData.forEach(function(g) {
        let a = {};
        a.name = g.name;
        a.children = [];
        g.children.forEach(function(item) {
            let obj = {};
            obj.datatype = item.datatype;
            obj.id = item.id;
            obj.name = item.name;

            //使用SdcardNumber代替字符
            if (string(obj.datatype) === "IMGPAK" || string(obj.datatype) === "DEM数据") {
                const str = item.path.split("/");
                const index = str[5];
                str[5] = sdcard;
                item.path = str[0] + "/" + str[1] + "/" + str[2] + "/" + str[3] + "/" + str[4] + "/" + str[5] + "/" + str[6] + "/" + str[7] + "/" + str[8] + "/" + str[9];
            } else {
                const str1 = item.path.split("/");
                const index1 = str1[2];
                str1[2] = sdcard;
                item.path = str1[0] + "/" + str1[1] + "/" + str1[2] + "/" + str1[3] + "/" + str1[4] + "/" + str1[5] + "/" + str1[6];
            }

            obj.path = item.path;
            obj.visible = item.cesiumTreeData._data.visible;
            if (item.cesiumTreeData.properties) {
                obj.properties = item.cesiumTreeData.properties;
            }
            a.children.push(obj);
        });
        resultLayer.push(a);
    });
    /*resultLayer.forEach(function(g) {
        g.children.forEach(function(item) {
            if (item.datatype === LayerManager.GEOJSON) {
                if (item.properties.type) {
                    delete item.properties.type;
                }
            }
        });
    });*/
    JavaContext.saveFile(this.pathObject, JSON.stringify(resultLayer));
};
//更新图层对象
LayerManager.prototype.updateJsonData = function(layerData) {
    let that = this;
    layerData.forEach(function(g) {
        g.children.forEach(function(item) {
            if (!item.cesiumTreeData) {
                item.cesiumTreeData = that.getCesiumTreeData(item);
            }
        });
    });
    that._jsonData = layerData;
};

//给pathObject赋值
LayerManager.prototype.setPathObject = function(path) {
    this.pathObject = path;
};

LayerManager.prototype.getPathObject = function() {
    return this.pathObject;
};
LayerManager.prototype.getVisibleLineLayer = function() {
    let ls = [];
    this._jsonData.forEach(function(g) {
        g.children.forEach(function(item) {
            if (item.cesiumTreeData) {
                if (item.cesiumTreeData._data.datatype === LayerManager.GEOJSON && item.cesiumTreeData._data.visible) {
                    if (item.cesiumTreeData._vectorType === LayerManager.GEOJSON_POLYLINE) {
                        ls.push(item);
                    }
                }
            }
        });
    });
    return ls;
};

export default LayerManager;
